package cn.maidouya.pet.controller;

import cn.maidouya.basic.exception.BusinessExcepiton;
import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.LoginContext;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.pet.domain.Pet;
import cn.maidouya.pet.domain.SearchMasterMsg;
import cn.maidouya.pet.query.SearchMasterMsgQuery;
import cn.maidouya.pet.service.IPetService;
import cn.maidouya.pet.service.ISearchMasterMsgService;
import cn.maidouya.user.domain.Logininfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IPetService petService;

    /**
     *   宠物寻主
     * @param searchMasterMsg  寻主信息
     * @param request 当前登录人
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        System.out.println("===================" +searchMasterMsg);
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.publish(searchMasterMsg,logininfo.getId());
            return AjaxResult.getResult();
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("系统异常！");
        }
    }
    // 待处理消息 pending
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        PageList<SearchMasterMsg> pageList = searchMasterMsgService.pending(searchMasterMsgQuery,logininfo.getId());
        return pageList;
    }
    // 已处理消息 processed
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        PageList<SearchMasterMsg> pageList = searchMasterMsgService.processed(searchMasterMsgQuery,logininfo.getId());
        return pageList;
    }
    // 用户自己消息
    @PostMapping("/list")
    public PageList<SearchMasterMsg> userList(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        PageList<SearchMasterMsg> pageList = searchMasterMsgService.userList(searchMasterMsgQuery,logininfo.getId());
        return pageList;
    }
    // 寻主消息处理
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            // 获取当前登录人信息
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.handle(pet,logininfo.getId());
            return AjaxResult.getResult();
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("系统异常");
        }
    }

}